<template>
	<view>
		<!-- <view style="display: flex; margin-top: 100rpx; margin-bottom: 25rpx; align-items: center;">
			<image @click="fanhui" style="width: 40rpx; height: 40rpx; margin-left: 20rpx;"
				src="../../../static/home/zuo.png" mode=""></image>
			<view style="font-size: 35rpx; text-align: center; width: 86%;">消息</view>
		</view> -->

		<!-- <view class="d-flex jc-center menu">
			<view :class="menu_index==0?'action':''" style="border-radius: 20rpx 0rpx 0rpx 20rpx;"
				@click="menu_index=0">公告信息</view>
			<view :class="menu_index==1?'action':''" style="border-radius: 0rpx 20rpx 20rpx 0rpx;"
				@click="menu_index=1">通知消息</view>
		</view> -->

		<view class="header gui-border gui-bg-white gui-flex gui-align-items-center gui-text" v-if="menu_index==0">
			<text class="gui-block gui-margin-x">{{total>0?total:0}} 条未读</text>
			<text class="gui-block gui-margin-x" :class="total>0?'gui-color-blue':'gui-color-gray'" @click="readAll"
				style="">标记全部已读</text>
		</view>

		<view class="content gui-bg-white gui-margin-top">
			<view :class="item.msg_log_count== 0 ? 'content-item' : 'no'" class="gui-border-b"
				v-for="(item,index) in messageList" :key="index" @click="gotoDetail(item,1)">
				<view class="gui-ellipsis mt-10 gui-bold">{{item.info_theme}}</view>
				<view class="gui-ellipsis gui-padding-small gui-text">
					{{item.info_text}}
				</view>
				<view class="margin-y gui-text d-flex jc-between p-5">
					<view>
						{{item.crt_time}}
					</view>
					<view>
						{{get_type(item)}}
					</view>
				</view>
			</view>
			<view class="text-center gui-color-gray gui-margin-top" @click="load_more1()">{{load_more_text1}}</view>
		</view>

		<!-- <view class="content gui-bg-white gui-margin-top" v-if="menu_index==1">
			<navigator :class="item.lock_state== 0 ? 'content-item' : 'no'" class="gui-border-b"
				v-for="(item,index) in myList" :key="index" :url="'/pages/home/notice/notice?title=通知&id='+item.id">
				<view class="gui-ellipsis gui-padding-small gui-text">
					{{item.title}}
				</view>
				<view class="margin-y gui-text d-flex jc-between p-5">
					<view>
						{{item.crt_time}}
					</view>

				</view>
			</navigator>
			<view class="text-center gui-color-gray gui-margin-top" @click="load_more2()">{{load_more_text2}}</view>
		</view> -->

	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: null,
				page: 1,
				page2: 1,
				pageSize: 10,
				load_more_text1: "点击加载更多",
				load_more_text2: "点击加载更多",
				messageList: [],
				myList: [],
				count: 0,
				total: 0,
				menu_index: 0,
				// app消息红点提醒
				unread: 0,
				is_push: false,
				total: null,
			}
		},
		onLoad(opt) {
			this.user = uni.getStorageSync('userInfo')
			if (opt.type) {
				this.menu_index = opt.type
			}
		},
		onShow() {
			this.messageList = [],
			this.myList = [],
			this.getMessage()
			// this.my_notice();

			//this.getCount()

		},
		mounted() {
			setTimeout(() => {
				this.current = 5
			}, 3000)
		},
		methods: {
			get_type(item) {

				switch (item.info_type) {
					case "11":
						return "新闻通知"
						break;
					case "12":
						return "公告通知"
						break;
					case "13":
						return "活动通知"
						break;
				}
			},

			load_more1() {
				if (this.load_more_text1 != "没有更多了") {
					this.page++;
					this.getMessage();
				}
			},
			load_more2() {
				if (this.load_more_text2 != "没有更多了") {
					this.page2++;
					this.my_notice();
				}
			},

			getMessage() {
				let self = this;
				let data = {
					page: this.page,
					pageSize: this.pageSize,
					ttid: self.user.id
				}
				this.$boya.NGet("/app/msg_info/query_page", data, (res) => {
					// console.log("res.data",res.data.rows)
					self.total = res.data.total
					// #ifdef APP
					plus.runtime.setBadgeNumber(self.total);
					// #endif

					
					if (res.data.rows.length > 0) {
						// console.log("total",this.total)
						//self.messageList = [...this.messageList, ...res.data.rows]
						res.data.rows.map(d => {
							self.messageList.push(d)
						})
					}else{
						self.load_more_text1 = "没有更多了"
					}
					console.log("messageList", this.messageList)
				})
			},
			my_notice() {

				let data = {
					page: this.page2,
					pageSize: this.pageSize
				}
				
				console.log("my_notice", data);
				let self = this;
				this.$boya.NPost("/app/my_notice", data, (res) => {
					console.log("my_notice  res", res)
					if (res.data.length == 0 || res.data.length < this.pageSize) {
						self.load_more_text2 = "没有更多了"
					}
					if (res.data.length > 0) {
						self.myList = [...this.myList, ...res.data]
					}

					// console.log("my_notice", self.myList)
				})
			},
			gotoDetail(e, t) {
				console.log('e: ', e)
				uni.navigateTo({
					url: "/pages/home/message/msgDetail?id=" + e.id + "&info_type=" + e.info_type + "&type=" + t
				})
			},
			getCount() {
				this.$lg.Get("/APP/msgInfo/getUnReadCount", {}, (res) => {
					// console.log("res",res)
					this.count = res.data
				})
			},
			readAll() {
				if (this.total <= 0) {
					this.$boya.msg("没有未读消息")
				} else {
					this.$lg.Get("/APP/msgInfo/readAll", {}, (res) => {
						if (res.code == '1') {
							this.getMessage();
							this.$boya.msg("全部已读成功")
							uni.$emit('get_unread');
						}
						console.log(res)
					})
				}
			},
			// onReachBottom() {
			// 	let self = this
			// 	console.log(this.page, this.pageSize, self.total)
			// 	console.log("我触底了")
			// 	if (self.page * self.pageSize >= self.total) {
			// 		uni.showToast({
			// 			title: "没有更多数据了",
			// 			icon: "none",
			// 			duration: 1000
			// 		});
			// 		setTimeout(() => {
			// 			uni.hideLoading()
			// 		}, 500)
			// 	} else {
			// 		if (self.page <= self.page - 1) {
			// 			setTimeout(() => {
			// 				uni.hideLoading()
			// 			}, 500)
			// 		} else {
			// 			uni.showLoading({
			// 				title: "加载中"
			// 			});
			// 			self.page++
			// 			self.getMessage()
			// 			this.messageList = this.messageList.concat(this.messageList)
			// 		}
			// 		setTimeout(() => {
			// 			uni.hideLoading()
			// 		}, 500)
			// 	}
			// },
			get_unread() {
				let self = this;
				// #ifdef APP-PLUS
				this.$boya.NGet("/app/msg_info/unread", {}, (res) => {
					let msgcount = uni.getStorageSync("msgCount");
					if (msgcount != res.data) {
						uni.setStorageSync("msgCount", res.data);
						// let nav1 = self.$refs.page1.get_ref("nav1");
						// nav1[0].badge(3, res.data);
						console.log("#####get_unread->res", res.data)
						self.unread = res.data

						plus.runtime.setBadgeNumber(res.data);
						if (self.is_push == false) {
							self.is_push = true;
							plus.push.createMessage("您有" + res.data + "条信息未读", {}, {
								cover: true,
								title: '消息提醒',
								sound: 'system'
							});
						}
					}
				}, null, {
					showLoading: false
				})
				// #endif
			},
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100vh;
		/* background-color: rgb(243, 245, 248); */
	}

	.header {
		width: 100%;
		height: 80rpx;
		justify-content: space-between;
	}

	.content {
		/* padding-top: 1rpx; */
		height: 100%;

	}

	.content-item {
		color: #000;
		width: 90%;
		margin: 5%;
	}

	.no {
		color: #c6c6c6;
		width: 90%;
		margin: 5%;
	}

	.margin-y {
		margin-bottom: 10rpx;
	}

	.m-y-20 {
		margin-top: 15rpx;
	}

	.menu {
		margin-bottom: 10rpx;
	}

	.menu view {
		width: 200;
		border: solid 1rpx #c6c6c6;
		padding: 10rpx 40rpx;
	}

	.menu .action {
		background-color: #d30704;
		color: white;
	}
</style>